<%--
  Created by IntelliJ IDEA.
  User: John
  Date: 2019/10/29
  Time: 16:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!-- 左侧导航区域（可配合layui已有的垂直导航） -->
<ul class="layui-nav layui-nav-tree"  lay-filter="left_nav">
    <%--<li class="layui-nav-item layui-nav-itemed">
        <a class="" href="javascript:;">所有商品</a>
        <dl class="layui-nav-child">
            <dd><a href="javascript:;">列表一</a></dd>
            <dd><a href="javascript:;">列表二</a></dd>
            <dd><a href="javascript:;">列表三</a></dd>
            <dd><a href="">超链接</a></dd>
        </dl>
    </li>
    <li class="layui-nav-item">
        <a href="javascript:;">解决方案</a>
        <dl class="layui-nav-child">
            <dd><a href="javascript:;">列表一</a></dd>
            <dd><a href="javascript:;">列表二</a></dd>
            <dd><a href="">超链接</a></dd>
        </dl>
    </li>
    <li class="layui-nav-item"><a href="">云市场</a></li>
    <li class="layui-nav-item"><a href="">发布商品</a></li>--%>
</ul>
<script type="text/javascript">
    // 动态加载左侧菜单
    layui.use(["jquery", "element", "layer"], function(){

        var $ = layui.jquery
            ,element = layui.element
            ,layer = layui.layer;

        $.getJSON("/get_permissions", function(data){
            if(!data || data.code != 200) {
                layer.alert("获取菜单数据出错!");
                return;
            }
            var permissionTree = data.data;

            buildMenu(permissionTree);

            element.render('nav', 'left_nav');
        });

        function buildMenu(permissionTree) {
            for(var idx in permissionTree) {
                var permission = permissionTree[idx];
                var children = permission.children; // 子菜单
                var id = permission.id; // 菜单ID
                var privilege = permission.privilege;   // 菜单名称
                var menuUrl = "javascript:;";
                // var url = permission.url;
                var url = 'https://www.baidu.com';
                if(!children || children.length < 1) {
                    $("ul[lay-filter='left_nav']").append("<li class=\"layui-nav-item\"><a href=\""+url+"\">"+privilege+"</a></li>");
                } else {
                    var li = $("<li></li>");
                    li.addClass("layui-nav-item");
                    if(idx == 0) {
                        li.addClass("layui-nav-itemed");
                    }
                    var a = $("<a></a>");
                    a.attr("href", menuUrl);
                    a.text(privilege);
                    li.append(a);
                    var dl = $("<dl></dl>");
                    dl.addClass("layui-nav-child");
                    for(var idx1 in children) {
                        var child_permission = children[idx1];
                        var id = child_permission.id; // 菜单ID
                        var privilege = child_permission.privilege;   // 菜单名称
                        dl.append("<dd><a href=\""+url+"\">"+privilege+"</a></dd>");
                    }

                    li.append(dl);
                    $("ul[lay-filter='left_nav']").append(li);
                }
            }
        }
    });
</script>
